﻿using CMS.BaseEntity;
using ElectronicSelling.BusinessLogic;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ElectronicSelling.Utilities;

namespace ElectronicSelling
{
    public partial class ProductList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Request.QueryString["action"] == "search")
                {
                    SearchProduct();
                }
                else
                    GetProductList();
            }
        }

        protected void GetProductList()
        {
            if (Request.QueryString["sid"] != null)
            {
                Product product = new Product();
                if (Request.QueryString["cid"] == null)
                {
                    try
                    {
                        product.SectionId = Convert.ToInt32(Request.QueryString["sid"]);
                        DataTable dt = new DataTable();
                        dt = HomeProductLogic.GetProductBySection(product);
                        Library.PaginateRepeater(dt, rptProduct, rptPage, PageNumber, ConstantDefault.Page_Size);
                    }
                    catch (Exception)
                    {
                        Response.Redirect(Directory.Error);
                    }
                }
                else
                {
                    Category category = new Category();
                    Section section = new Section();
                    category.CategoryId = Convert.ToInt32(Request.QueryString["cid"]);
                    section.SectionId = Convert.ToInt32(Request.QueryString["sid"]);
                    DataTable dt = new DataTable();
                    dt = HomeProductLogic.GetProductByCategory(category, section);
                    Library.PaginateRepeater(dt, rptProduct, rptPage, PageNumber, ConstantDefault.Page_Size);
                }
            }
            else
            {
                DataTable dt = new DataTable();
                dt = HomeProductLogic.GetMostViewList();
                Library.PaginateRepeater(dt, rptProduct, rptPage, PageNumber, ConstantDefault.Page_Size);
            }
        }

        protected string GetPathSection()
        {
            if (Request.QueryString["sid"] != null)
            {
                try
                {
                    Section section = new Section();
                    section.SectionId = Convert.ToInt32(Request.QueryString["sid"].ToString());
                    return HomeProductLogic.GetSectionById(section);
                }
                catch (Exception)
                {
                    return null;
                }
            }
            return null;
        }

        protected string GetPathCategory()
        {
            if (Request.QueryString["cid"] != null)
            {
                try
                {
                    Category category = new Category();
                    category.CategoryId = Convert.ToInt32(Request.QueryString["cid"].ToString());
                    return HomeProductLogic.GetCategoryById(category);
                }
                catch (Exception)
                {
                    return null;
                }
            }
            return null;
        }

        public string GetMenuMultiLevel()
        {
            Category cate = new Category();
            DataTable dtSection = new DataTable();
            dtSection = HomeProductLogic.GetSection();
            string html = "";
            if (dtSection.Rows.Count > 0)
            {
                foreach (DataRow rowSection in dtSection.Rows)
                {
                    cate.SectionId = Convert.ToInt32(rowSection[0]);
                    DataTable dtCategory = new DataTable();
                    dtCategory = HomeProductLogic.GetCategoryBySection(cate);
                    html += "<li>";
                    html += "<a class='section' href='ProductList.aspx?sid=" + rowSection[0] + "'>" + rowSection[1] + "</a>";
                    html += "<hr />";
                    if (dtCategory.Rows.Count > 0)
                    {
                        html += "<ul>";
                        foreach (DataRow rowCategory in dtCategory.Rows)
                        {
                            html += "<li style='list-style-type: disc' >";
                            html += "<a href='ProductList.aspx?sid=" + rowSection[0] + "&cid=" + rowCategory[0] + "'>";
                            html += rowCategory[1];
                            html += "</a>";
                            html += "</li>";
                            html += "<div class='line_break'></div>";
                        }
                        html += "</ul>";
                    }
                    html += "</li>";
                }
            }
            return html;
        }

        protected void SearchProduct()
        {
            if (Request.QueryString["action"] == "search")
            {
                string searchValue = Session["SearchValue"].ToString();
                DataTable dt = HomeProductLogic.SearchProductAll(searchValue);
                Library.PaginateRepeater(dt, rptProduct, rptPage, PageNumber, ConstantDefault.Page_Size);
            }
        }

        public int PageNumber
        {
            get
            {
                if (ViewState["PageNumber"] != null)
                    return Convert.ToInt32(ViewState["PageNumber"]);
                else
                    return 0;
            }
            set
            {
                ViewState["PageNumber"] = value;
            }
        }

        protected void rptPages_ItemCommand1(object source, RepeaterCommandEventArgs e)
        {
            PageNumber = Convert.ToInt32(e.CommandArgument) - 1;
            GetProductList();
        }
    }
}